/* @(#)GSOrderDetailQueryConfirmAction.java
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-2-23     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun,
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.text.DecimalFormat;
import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrderProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSOrderDetailQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.page.ResultProcess;

/**
 * 巴士物资人员执行订单明细查询
 */
public class GSOrderDetailQueryConfirmAction extends GSBaseAction {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
	 */
	public String execute(GSBaseForm aForm) throws Exception {
		GSOrderDetailQueryForm tForm = (GSOrderDetailQueryForm) aForm;
		try {
			tForm.getRequest().setAttribute("orderid", tForm.getOrderid());
			tForm.getRequest().setAttribute("providerName", tForm.getProviderName());
			tForm.getRequest().setAttribute("concentricityId", tForm.getConcentricityId());
			tForm.getRequest().setAttribute("orderCreateDateStr", tForm.getOrderCreateDateStr());
			tForm.getRequest().setAttribute("orderCreateDateEnd", tForm.getOrderCreateDateEnd());
			tForm.getRequest().setAttribute("manufInfo", tForm.getManufInfo());
			tForm.getRequest().setAttribute("orderDetailStatus", tForm.getOrderDetailStatus());
			tForm.getRequest().setAttribute("deliverGoodsId", tForm.getDeliverGoodsId());
			tForm.getRequest().setAttribute("orderType", tForm.getOrderType());
			tForm.getRequest().setAttribute("drawingpiececode", tForm.getDrawingpiececode());
			tForm.getRequest().setAttribute("allStatus", tForm.getAllstatus());//9/10/12状态

			String[] tDateStr = new String[2];
			tDateStr[0] = tForm.getOrderCreateDateStr();
			tDateStr[1] = tForm.getOrderCreateDateEnd();
			tDateStr = DateUtil.formateDate(tDateStr);
			tForm.setOrderCreateDateStr(tDateStr[0]);
			tForm.setOrderCreateDateEnd(tDateStr[1]);

			LoginContext context = (LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);

			GSUniteGwProcessor unitePro = new GSUniteGwProcessor();
			ArrayList wareList = unitePro.getWareHouseLevelName(context.getLoginWarehouseCode(),context.getLoginWarehouseCode());
			aForm.getRequest().setAttribute("orderdept", wareList);
			GSOrderProcessor tProc=new GSOrderProcessor();
			//取得分页参数
			String[] tStr = getPageParams(tForm);
			//执行分页查询
			ArrayList tList = tProc.getOrderDetailList(tForm,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
			ResultProcess tRsProc = new ResultProcess();
			tRsProc.setResult(tList);
			tForm.getRequest().setAttribute("order", tRsProc);

			//存储分页参数
			tForm.getRequest().setAttribute("pageSize",tStr[1]);
			tForm.getRequest().setAttribute("totalRecords",tStr[2]);

			//取得订单明细状态
			aForm.getRequest().setAttribute("orderdetailstatuslist", getOrderDetailStatusList());

			//取得订单明细状态
			aForm.getRequest().setAttribute("ordertypelist", getOrderTypeList());

			// 取得订单总金额
			double tTotalAmount = tProc.getOrderDetailTotalAmount(tForm);
			DecimalFormat df = new DecimalFormat("##0.00##");
			String totalAmount = df.format(tTotalAmount);
			aForm.getRequest().setAttribute("totalAmount", totalAmount);

			//取得产品分类
			GSManufactureProcessor tManufProc = new GSManufactureProcessor();
			tManufProc.getManufactureOfLevel(aForm);

		} catch (Exception e) {
			tForm.getRequest().setAttribute("action", "A150501.do");
			throw e;
		}
		return GSBaseConstants.FORWARD_SUCCESS;
	}

	/**
	 * 取得订单明细状态
	 * @return 订单明细状态列表
	 */
	private ArrayList getOrderDetailStatusList() {
		ArrayList tResult = new ArrayList();

		String [] tArray = new String[2];
		tArray[0] = "6";
		tArray[1] = "已提交";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "7";
		tArray[1] = "已核准";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "8";
		tArray[1] = "未核准";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "9";
		tArray[1] = "订货中";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "10";
		tArray[1] = "已确认";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "11";
		tArray[1] = "已拒绝";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "12";
		tArray[1] = "交货确认";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "14";
		tArray[1] = "已完成";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "21";
		tArray[1] = "已取消";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "22";
		tArray[1] = "已作废";
		tResult.add(tArray);

		return tResult;
	}

	private ArrayList getOrderTypeList() {
		ArrayList tResult = new ArrayList();
		String [] tArray = new String[2];
		tArray[0] = "0";
		tArray[1] = "正常采购";
		tResult.add(tArray);

		tArray = new String[2];
		tArray[0] = "1";
		tArray[1] = "应急采购";
		tResult.add(tArray);

		return tResult;
	}
	private String[] getPageParams(GSOrderDetailQueryForm aForm) throws Exception{
		String tStr[] = new String[3];
		int totalRecords;
		int pageNo;
		int pageSize;
		String tPage = GSBaseConstants.tPage;
		//根据页面传入的查询参数查询订单信息
		if(aForm.getChoice() == null || aForm.getChoice().equals("")){
			GSOrderDAO tDAO = new GSOrderDAO();
			totalRecords = tDAO.getOrderDetailListCount(aForm);

			// 取得系统默认的页大小
			pageSize = Integer.parseInt(tPage);
			// PageNo=1
			pageNo = 1;
			int n = totalRecords % pageSize == 0? totalRecords / pageSize:totalRecords / pageSize + 1;
			if (pageNo>n) {
				pageNo=n;
			}
			tStr[0] = "" + pageNo;
			tStr[1] = "" + pageSize;
			tStr[2] = "" + totalRecords;
		}else{
			tStr = GSBaseManager.getPageNoAndPageSize(aForm);
		}
		return tStr;
	}

}
